var currTab = "sys_tab";

function drawCharts(page, start, end, interval, multiple, ctags) {
    $.each(charts_group[page], function (index, val) {
        drawChart(config[val], page, lineChart, start, end, interval, multiple, ctags);
    });
}

function drawTableCharts(page, table, start, end, interval, multiple, ctags) {
    $.each(charts_group[page], function (index, val) {
        drawaTableChart(config[val], page, table, lineChart, start, end, interval, multiple, ctags);
    });
}

function drawaTableChart(conf, tab, table, chart, start, end, interval, multiple, ctags) {
    setTimeout(function () {
        chart.title.text = table + "__" + conf.id;
        chart.yAxis.title.text = conf.y_title;
        chart.chart.events.load = function () {
            var ss = this.series;
            setInterval(function () {
                if (tab == currTab) {
                    // console.log("Tab is not visible, skip current updating.");
                    return;
                }
                $.each(ss, function (index, val) {
                    console.log(val);
                    var metric = "table." + table + "." + conf.series[val.name].metric;
                    var startTime = new Date().getTime() / 1000 - (60 * 60 * multiple);
                    var endTime = new Date().getTime() / 1000 - interval * multiple;
                    var tags = conf.series[val.name].tags;
                    $.each(ctags, function(k, v){
                        tags[k] = v;
                    });
                    var aggregator = conf.aggregator;
                    var postData = HmonRequest.buildPostData(metric, parseInt(startTime), parseInt(endTime), tags, aggregator, multiple);
                    HmonRequest.updateSeries(val, postData);
                });
            }, interval * 1000);
        };
        var id = table + "__" + conf.id;
        $('#' + id).highcharts(chart);
        var hchart = $('#' + id).highcharts();
        initTableSeries(conf, hchart, table, start, end, multiple, ctags);
    }, 200)
}

function drawChart(conf, tab, chart, start, end, interval, multiple, ctags) {
    setTimeout(function () {
        chart.title.text = conf.title;
        chart.yAxis.title.text = conf.y_title;
        chart.chart.events.load = function () {
            var ss = this.series;
            setInterval(function () {
                if (tab == currTab) {
                    return;
                }
                $.each(ss, function (index, val) {
                    var metric = conf.series[val.name].metric;
                    var startTime = new Date().getTime() / 1000 - (60 * 60 * multiple);
                    var endTime = new Date().getTime() / 1000 - interval * multiple;
                    var tags = conf.series[val.name].tags;
                    $.each(ctags, function(k, v){
                        tags[k] = v;
                    });
                    var aggregator = conf.aggregator;
                    var postData = HmonRequest.buildPostData(metric, parseInt(startTime), parseInt(endTime), tags, aggregator, multiple);
                    HmonRequest.updateSeries(val, postData);
                });
            }, interval * 1000);
        };
        var id = conf.id;
        $('#' + id).highcharts(chart);
        var hchart = $('#' + id).highcharts();
        initSeries(conf, hchart, start, end, multiple, ctags);
    }, 200)
}

function initSeries(conf, chart, start, end, multiple, ctags) {
    var json = conf.series;
    for (var name in json) {
        var tags = json[name].tags;
        $.each(ctags, function(k, v){
            tags[k] = v;
        });
        var postData = HmonRequest.buildPostData(json[name].metric, parseInt(start), parseInt(end), tags, conf.aggregator, multiple);
        HmonRequest.initSeries(chart, name, postData);
    }
}

function initTableSeries(conf, chart, table, start, end, multiple, ctags) {
    var json = conf.series;
    for (var name in json) {
        var tags = json[name].tags;
        $.each(ctags, function(k, v){
            tags[k] = v;
        });
        var metric = "table." + table + "." + json[name].metric;
        var postData = HmonRequest.buildPostData(metric, parseInt(start), parseInt(end), tags, conf.aggregator, multiple);
        HmonRequest.initSeries(chart, name, postData);
    }
}
